package es.cazadoresmadridejos.beans.dao.city.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.city.CityDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.City;

@SuppressWarnings("unchecked")
@Repository("cityDao")
public class CityDaoImpl extends DaoImpl<City> implements CityDao{

	@Override
	public City newInstance() {
		return new City();
	}

	@Override
	public List<City> findByProvince(final Long id) {
		return (List<City>) getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				StringBuilder hql = new StringBuilder();
				hql.append("select c from City c where c.province.id = :idProvince");
				
				
				Query query = session.createQuery(hql.toString());

				query.setParameter("idProvince", id);
				
				return query.list();
			}
		});
	}

}
